package edu.ynu.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class JsonConvert { 
	public static JSONObject message(String message, boolean success) 
	{         
		Map map = new HashMap();          
		map.put("success", success);         
		map.put("message", message);         
		return JSONObject.fromObject(map); 
	}
	public static JSONObject generate(List list) 
	{     
		Map map = new HashMap();    
		map.put("rowCount", list.size());    
		map.put("data", list);        
		return JSONObject.fromObject(map); 
	}
	public static JSONObject javabean2json(Object object, String message, boolean success) { 
		Map map = new HashMap();         
		map.put("success", success);       
		map.put("message", message);         
		map.put("data", object);         
		return JSONObject.fromObject(map); 
	}
	public static JSONObject objectcollect2json(List list, String total) {      
		Map map = new HashMap();          
		map.put("rowCount", total);    
		map.put("data", list);         
		return JSONObject.fromObject(map); 
	}
	public static JSONArray getJSONArrayFormString(String str) {   
		if (str == null || str.trim().length() == 0) {    
			return null;         
		}       
		JSONArray jsonArray = null;       
		try {           
			jsonArray = JSONArray.fromObject(str);       
		} catch (JSONException e) {      
			e.printStackTrace();        
			}         
		return jsonArray;
	}
	public static JSONObject StringToJSONOBject(String str) {    
		if (str == null || str.trim().length() == 0) {     
			return null;        
		}        
		JSONObject jsonObject = null;        
		try {          
			jsonObject = JSONObject.fromObject(str);         
			} catch (JSONException e) {   
				e.printStackTrace();       
				}       
		return jsonObject; 
		}
	}
